package com.example.smartagrisys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.smartagrisys.common.Result;
import com.example.smartagrisys.entity.SellerProduct;
import com.example.smartagrisys.service.SellerProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 肥料控制器
 */
@Slf4j
@RestController
@RequestMapping("/fertilizer")
public class FertilizerController {

    @Autowired
    private SellerProductService sellerProductService;

    /**
     * 获取肥料列表
     * @return 肥料列表
     */
    @GetMapping("/list")
    public Result<List<SellerProduct>> getFertilizerList() {
        log.info("获取肥料列表");

        // 查询已上架的商品
        LambdaQueryWrapper<SellerProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SellerProduct::getStatus, 1) // 只获取已上架的商品
                   .orderByDesc(SellerProduct::getCreateTime);

        List<SellerProduct> productList = sellerProductService.list(queryWrapper);

        // 确保初始化数组字段（suitableCropsList和certificatesList）
        for (SellerProduct product : productList) {
            product.initArrayFields();
        }

        log.info("获取到{}个肥料商品", productList.size());

        return Result.success(productList);
    }
}
